package com.gitee.service.impl;

import com.gitee.dto.CommentDTO;
import com.gitee.entity.Comment;
import com.gitee.entity.FriendUpdates;
import com.gitee.mapper.CommentMapper;
import com.gitee.mapper.FriendUpdatesMapper;
import com.gitee.service.CommentService;
import com.gitee.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private FriendUpdatesMapper friendUpdatesMapper;

    @Override
    public void save(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        commentMapper.insert(comment);
    }

    @Override
    @Transactional
    public List<CommentVO> getByFriendUpdatesId(Integer friendUpdatesId) {
        // 更新该动态的评论数量
        List<CommentVO> commentVOS = commentMapper.selectByFriendUpdateId(friendUpdatesId);

        if (commentVOS != null) {
            FriendUpdates friendUpdates = new FriendUpdates();
            friendUpdates.setId(friendUpdatesId);
            friendUpdates.setCommentCount(commentVOS.size());
            friendUpdatesMapper.updateById(friendUpdates);
        }
        return commentVOS;
    }
}
